/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pesqRuas.java
 *
 * Created on 05/01/2010, 22:12:40
 */

package cparc;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public class pesqRuas extends javax.swing.JInternalFrame {
    
        static String bCod;
        static cadastroProp frameProp;





     public void setProp(cadastroProp prop, String bairroCod){

        //System.out.println(bCod);
        frameProp = prop;
        bCod = bairroCod;
        //System.out.println(bCod);
        this.populaTabela();
    }

     public void populaTabela(){

        tfConsulta.setText("Digite a rua aqui...");
        tfConsulta.requestFocusInWindow();
        tfConsulta.selectAll();

        //bCod = frameProp.getcodBai();

        DefaultTableModel modelo = (DefaultTableModel) tbRua.getModel();
        Statement stm = null;
        String result = null;
        String consulta = "SELECT RCOD, RNOM FROM CPARC.CAD_RUAS WHERE RBAI = " + bCod + " ORDER BY RNOM ASC";
        System.out.println(consulta);
        int i=0;

        try  {
        stm = (Statement) conexao.getConnection(true).createStatement();
        ResultSet rs = stm.executeQuery(consulta);

        while(rs.next()){
        modelo.addRow(new String[] {Integer.toString(rs.getInt("RCOD")), rs.getString("RNOM")});
        modelo.fireTableCellUpdated(i, 0);
        modelo.fireTableCellUpdated(i, 1);
        i++;
        }

        }
        catch(Exception e){
            e.printStackTrace();
        }


     }

    /** Creates new form pesqRuas */
    public pesqRuas() {
        initComponents();
        //bCod = frameProp.getcodBai();
        
        //this.populaTabela();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfConsulta = new javax.swing.JTextField();
        btConsulta = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbRua = new javax.swing.JTable();

        setTitle("Pesquisa de Ruas");

        tfConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConsultaKeyPressed(evt);
            }
        });

        btConsulta.setText("Consultar");
        btConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConsultaActionPerformed(evt);
            }
        });

        tbRua.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Rua"
            }
        ));
        tbRua.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbRuaKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbRua);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btConsulta)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btConsulta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConsultaKeyPressed

        if(evt.getKeyCode() == 10){

            btConsulta.doClick();
        }
}//GEN-LAST:event_tfConsultaKeyPressed

    private void btConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConsultaActionPerformed

        //System.out.println("Botao");
        DefaultTableModel modelo = (DefaultTableModel) tbRua.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String result = null;
        String consulta = "SELECT RCOD, RNOM FROM CPARC.CAD_RUAS WHERE RBAI = "+ bCod+" AND RNOM LIKE '%" + tfConsulta.getText() + "%' ORDER BY RNOM ASC";
        int i=0;
        //System.out.println(consulta);
        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(consulta);

            while(rs.next()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("RCOD")), rs.getString("RNOM")});
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                i++;
            }

        } catch(Exception e){
            e.printStackTrace();
        }

        tbRua.requestFocusInWindow();
}//GEN-LAST:event_btConsultaActionPerformed

    private void tbRuaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbRuaKeyPressed

        if(evt.getKeyCode() == 10){

            this.dispose();
            frameProp.requestFocusInWindow();
            frameProp.rua((String) tbRua.getValueAt(tbRua.getSelectedRow(), 0), (String) tbRua.getValueAt(tbRua.getSelectedRow(), 1));
            
            }
}//GEN-LAST:event_tbRuaKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JButton btConsulta;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tbRua;
    public javax.swing.JTextField tfConsulta;
    // End of variables declaration//GEN-END:variables

}
